package leetbook.sort_search;

public class Solution287 {

    public static void main(String[] args) {

        int[] nums = {1,3,4,2,2};
        new Solution287().findDuplicate(nums);

    }

    public int findDuplicate(int[] nums) {
        //快慢指针
        int slow = nums[0], fast = slow;
        do{
            slow = nums[slow];
            fast = nums[fast];
            fast = nums[fast];
        }while(slow!=fast);

        //相等了，快指针回到头
        fast = nums[0];
        while(slow!=fast){
            slow = nums[slow];
            fast = nums[fast];
        }
        //第二次相遇处即为环入口
        return slow;
    }

}
